<template>
	<view>
		登录
	</view>
	<!-- #ifdef MP-WEIXIN -->
	<button @click="doLogin">微信授权登录</button>
	<button @click="getPosition">获取位置</button>
	<!-- #endif -->
	<!-- #ifndef MP-WEIXIN -->
	
	<button @click="doLogin">手机号登录</button>
	<!-- #endif -->
	
	
</template>

<script setup>
	import {
		getweixinOpenId,
		weixinLogin,
		getPositionMap
	} from "../../api/user.js"
	import config from "../../config/index.js";
	import { onShow } from "@dcloudio/uni-app"
	function doLogin() {
		
		// 微信授权登录
		uni.login({
			provider: 'weixin',
			success: function(loginRes) {
				// 获取code
				console.log(loginRes.code, 13);
				// 调接口 拿openid
				getweixinOpenId({
					code: loginRes.code,
					appid: config.APPID,
					secret: config.SECRET
				}).then(e => {
					// 这里拿到openid了
					console.log(e)
					// 获取用户信息
					uni.getUserInfo({
						provider: 'weixin',
						success: function(infoRes) {
							console.log(infoRes, 18)
							console.log('用户昵称为：' + infoRes.userInfo.nickName); //avatarUrl
							weixinLogin({
								openid: e.data.openid,
								headerurl: infoRes.userInfo.avatarUrl,
								nickname: infoRes.userInfo.nickName
							}).then(res => {
								console.log(res)
							})
						}
					});

				})

			}
		});

	}
	
	onShow(()=>{
		uni.getLocation({
			type: 'wgs84',
			success: function (res) {
				console.log('当前位置的经度：' + res.longitude);
				console.log('当前位置的纬度：' + res.latitude);
			}
		});
	})
	
	function getPosition(){
		console.log("走了")
		wx.getLocation({
		 type: 'wgs84',
		 success (res) {
			 console.log(res)
		   const latitude = res.latitude
		   const longitude = res.longitude
		   const speed = res.speed
		   const accuracy = res.accuracy
		   getPositionMap({lat:res.latitude,lng:res.longitude}).then(e=>{
			   console.log(e.result.address_component.city,85)
		   })
		   
		 },fail(err) {
		 	console.log(err,84)
		 }
		})
	}
</script>

<style>

</style>